package com.cn.pw.frame.utils;

/*
 * Copyright (c) 2008-2010.
 *
 * This software is ......
 */


import java.io.Serializable;

/**
 * AjaxResult have mainly wrapped the AJAX request.
 * 
 * @Id AjaxResult.java
 * @version
 * @author Leon ;
 * @Date Mar 27, 2009
 */
public class AjaxResult implements Serializable {

  /** serialVersionUID */
	private static final long serialVersionUID = -8388563740726644385L;

	/** AJAX request for submit. */
  public static final int AJAX_SUBMIT = 1;

  /** AJAX for get data. */
  public static final int AJAX_DATA = 2;

  /** success */
  private boolean success;

  /** data */
  private Object data;

  /** errorMessage */
  private String errorMessage;

  /** requestType */
  private int requestType;

  public boolean isSuccess() {
    return success;
  }

  public Object getData() {
    return data;
  }

  public int getRequestType() {
    return requestType;
  }

  public String getErrorMessage() {
    return errorMessage;
  }

  public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
  }

  public AjaxResult(boolean success, Object data) {
    this.success = success;
    this.data = data;
    requestType = 1;
  }

  public AjaxResult(boolean success, String errorMessage) {
    this.success = success;
    if (success) {
      this.data = errorMessage;
    } else {
      this.errorMessage = errorMessage;
    }
    requestType = 1;
  }

  public AjaxResult(boolean success) {
    this.success = success;
    requestType = 1;
  }

  public AjaxResult(Object data) {
    this.data = data;
    requestType = 2;
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  public String toString() {
    switch (requestType) {
    case AJAX_SUBMIT:
      return "success=" + success + ", data=" + data;
    case AJAX_DATA:
      return "data=" + data;
    default:
      return "Uncertain[" + requestType + "],success=" + success + ", data="
          + data;
    }
  }
}